package main

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
	"io"
	"log"
	"mxshop_srvs/inventory_srv/model"
	"os"
	"time"
)

func genMd5(code string) string {
	Md5 := md5.New()
	_, _ = io.WriteString(Md5, code)
	return hex.EncodeToString(Md5.Sum(nil))
}

func main() {
	dsn := "root:root@tcp(localhost:13316)/mxshop_inventory_srv?charset=utf8&parseTime=True&loc=Local"

	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), //io writer
		logger.Config{
			SlowThreshold: time.Second, //慢SQL 阈值
			LogLevel:      logger.Info, //Log level
			Colorful:      true,        //禁用彩色打印
		},
	)

	//全局模式
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
		Logger: newLogger,
	})
	if err != nil {
		panic(err)
	}
	//设置全局的logger 这个logger在我们执行每一个sql语句的时候都会打印每一行sql
	//sql 才是最重要的
	//定义一个表结构,将表结构直接生成对应的表- migrations
	// 迁移 schema
	//_ = db.AutoMigrate(&model.Inventory{}, &model.StockSellDetail{})

	//插入一条数据
	//orderDetail := model.StockSellDetail{
	//	OrderSn: "gitee-seina",
	//	Status:  1,
	//	Detail:  []model.GoodsDetail{{1, 3}, {2, 3}},
	//}
	//db.Create(&orderDetail)
	var sellDetail model.StockSellDetail
	_ = db.Where(model.StockSellDetail{
		OrderSn: "gitee-seina",
	}).First(&sellDetail)

	fmt.Println(sellDetail.Detail)
}
